﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Xml;
using System.Linq;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace MHWPA
{
    public class MHViewModel
    {
        public ObservableCollection<HawkItem> FeaturedListItems { get; set; }
        public ObservableCollection<CollectionItem> SermonListItems { get; set; }
        public ObservableCollection<CollectionItem> MusicListItems { get; set; }

        private WebClient client;
        private Uri[] uris;

        // Populate the lists on the main panorama
        public MHViewModel()
        {
            uris = new Uri[] { new Uri("http://www.marshillchurch.org/air/hawk.xml"), new Uri("http://www.marshillchurch.org/media/sermons.xml"), new Uri("http://www.marshillchurch.org/media/music.xml") };
        }

        public void RefreshUris()
        {
            foreach (Uri uri in uris)
            {
                client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

                client.DownloadStringAsync(uri, uri);
            }
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //if (e.Error == null)
            Uri uri = e.UserState as Uri;

            if (uri.ToString() == "http://www.marshillchurch.org/air/hawk.xml")
            {
                try
                {
                    XElement xmlFeed = XElement.Parse(e.Result);
                    foreach (XElement item in xmlFeed.Descendants("item"))
                    {
                        App.Model.FeaturedListItems.Add(
                            new HawkItem()
                            {
                                Title = item.Element("title").Value,
                                Image = item.Element("poster_image").Value
                            });
                    }
                }
                catch { }
            }

            if (uri.ToString() == "http://www.marshillchurch.org/media/sermons.xml")
            {
                try
                {
                    XElement xmlFeed = XElement.Parse(e.Result);
                    foreach (XElement item in xmlFeed.Descendants("collection_child"))
                    {
                        App.Model.SermonListItems.Add(
                            new CollectionItem()
                            {
                                Title = item.Element("title").Value,
                                Image = item.Element("iphone_image").Value,
                                PosterImage = item.Element("poster_img").Value,
                                DateRange = item.Element("date_range").Value,
                                Uri = item.Element("uri").Value
                            });
                    }
                }
                catch { }
            }

            if (uri.ToString() == "http://www.marshillchurch.org/media/music.xml")
            {
                try
                {
                    XElement xmlFeed = XElement.Parse(e.Result);
                    foreach (XElement item in xmlFeed.Descendants("collection_child"))
                    {
                        App.Model.MusicListItems.Add(
                            new CollectionItem()
                            {
                                Title = item.Element("title").Value,
                                Image = item.Element("iphone_image").Value,
                                PosterImage = item.Element("poster_img").Value,
                                DateRange = item.Element("date_range").Value,
                                Uri = item.Element("uri").Value
                            });
                    }
                }
                catch { }
            }
        }

        public class HawkItem
        {
            public string Title { get; set; }
            public string Image { get; set; }
        }

        public class CollectionItem
        {
            public string Title { get; set; }
            public string Image { get; set; }
            public string PosterImage { get; set; }
            public string DateRange { get; set; }
            public string Uri { get; set; }
        }
    }
}